package com.fhao.lotterysystem.service.Impl;

import com.fhao.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.fhao.lotterysystem.common.exception.ServiceException;
import com.fhao.lotterysystem.controller.param.UserRegisterParam;
import com.fhao.lotterysystem.dao.mapper.UserMapper;
import com.fhao.lotterysystem.service.UserService;
import com.fhao.lotterysystem.service.dto.UserRegisterDTO;
import com.fhao.lotterysystem.service.enums.UserIdentityEnum;
import com.fhao.lotterysystem.utils.RegexUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserRegisterDTO register(UserRegisterParam param) {


        UserRegisterDTO userRegisterDTO = new UserRegisterDTO();
        userRegisterDTO.setUserId(12l);
        return userRegisterDTO;
    }
    private void checkRegisterInfo(UserRegisterParam param) {
        //非空校验
        if(null == param) {
            throw new ServiceException(ServiceErrorCodeConstants.REGISTER_INFO_IS_EMPTY);
        }
        //校验邮箱格式 xxx@xxx.xxx
        if(!RegexUtil.checkMail(param.getMail())) {
            throw new ServiceException(ServiceErrorCodeConstants.MAIL_ERROR);
        }
        //校验手机号格式
        if(!RegexUtil.checkMobile(param.getPhoneNumber())) {
            throw new ServiceException(ServiceErrorCodeConstants.PHONE_NUMBER_ERROR);
        }
        //校验身份信息
        if(null == UserIdentityEnum.forName(param.getIdentity())) {
            throw new ServiceException(ServiceErrorCodeConstants.IDENTITY_ERROR);
        }
        //校验管理员密码必填
        if(param.getIdentity().equalsIgnoreCase(UserIdentityEnum.ADMIN.name())
                && !StringUtils.hasText(param.getPassword())) {
            throw new ServiceException(ServiceErrorCodeConstants.PASSWORD_IS_EMPTY);
        }
        //密码校验,至少6位
        if(StringUtils.hasText(param.getPassword()) && !RegexUtil.checkPassword(param.getPassword())) {
            throw new ServiceException(ServiceErrorCodeConstants.PASSWORD_ERROR);
        }
        //校验邮箱是否被使用
        if(checkMailUsed(param.getMail())) {
            throw new ServiceException(ServiceErrorCodeConstants.MAIL_USED);
        }
        //校验手机号是否被使用
        if(checkPhoneNumberUsed(param.getPhoneNumber())) {

        }
    }

    private boolean checkPhoneNumberUsed(String phoneNumber) {

    }

    private boolean checkMailUsed(String mail) {
        int count = userMapper.countByMail(mail);
        return count > 0;
    }
}























